package cepicam.src.tools;

import java.awt.Image;
import java.util.GregorianCalendar;

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import cepicam.src.view.DialogLectureCarteEid;

import be.belgium.eid.*;

public final class EidReaderNew {



	private static String nom;
	private static String prenom ;
	private static String lieu_naiss;
	private static String date_naiss;
	private static String sexe;
	private static String nationalite;
	private static String reg_nat;
	private static String rue;
	private static String code_postal;
	private static String ville;
	private static String pays;
	private static JOptionPane pane;
	private static ImageIcon ic;

    

	
	public static void read(){
		

		
		nom = null;
	    prenom  = null ;
	    lieu_naiss  = null;
	    date_naiss = null;
	    sexe = null;
	    nationalite = null;
	    reg_nat = null;
	    rue = null;
	    code_postal = null;
	    ville = null;
	    pays = null;
	    
		 try
	        {
	            System.loadLibrary("beidlibjni");
	        }
	        catch (UnsatisfiedLinkError e)
	        {
	          System.err.println("Native code library eidlib failed to load.\n" + e);
	          System.exit(1);
	        }
		 
		 
		 BEID_Long beLong = new BEID_Long();
	        BEID_Status CardStatus;
	        BEID_ID_Data idData = new BEID_ID_Data();
	        BEID_Certif_Check CertCheck = new BEID_Certif_Check();
	        BEID_Bytes userPicture = new BEID_Bytes();
	        BEID_Address userAddress = new BEID_Address();
	        BEID_VersionInfo versionInfo = new BEID_VersionInfo();
	        
	        CardStatus = eidlib.BEID_Init(null,0,0,beLong);
	        if(CardStatus.getGeneral() == 0){
	            System.out.println("You are been connected.");

	        }
	        else{
	        	System.out.println("Connection fail !");

	        }
	        
	        System.out.println("<Connected> Reading the card ...");

	      
	       
	        eidlib.BEID_GetID(idData,CertCheck);
	        
	        // User
	        
	        nom = idData.getName();
	        prenom = idData.getFirstName1();
	        lieu_naiss = idData.getBirthLocation();
	        date_naiss = FormatDate(idData.getBirthDate());
	        sexe = idData.getSex();
	        nationalite = idData.getNationality();
	        reg_nat = idData.getNationalNumber();
	        eidlib.BEID_GetAddress(userAddress, CertCheck);
	        rue = userAddress.getStreet();
	        code_postal = userAddress.getZip();
	        ville = userAddress.getMunicipality();
	        pays = userAddress.getCountry();
	        ic = null;

	        CardStatus = eidlib.BEID_GetPicture(userPicture, CertCheck);
	        if(CardStatus.getGeneral() != 0)
	            System.out.println("you are been fucked");
	        
	        ic = new ImageIcon(userPicture.getData());
	        ic.getImage();//S'assurer que l'image est complètement chargée
	        
	        // Card
	        
	        String carte_num = idData.getCardNumber();
	        String chip_num = idData.getChipNumber();
	        String validiteDebut = FormatDate(idData.getValidityDateBegin());
	        String validiteFin = FormatDate(idData.getValidityDateEnd());
	        
	        BEID_Bytes signedStatus = new BEID_Bytes();
	        CardStatus = eidlib.BEID_GetVersionInfo(versionInfo,CertCheck.getSignatureCheck(),signedStatus);
	        if(CardStatus.getGeneral() != 0)
	            System.out.println("No version info available");
	        
	        System.out.println(CertCheck.getCertificatesLength());
	        System.out.println(CertCheck.getCertificate(0).getCertifLabel());
	        System.out.println(CertCheck.getCertificate(0).getCertif());
	        System.out.println("SIGNATURE : " + CertCheck.getSignatureCheck());
	        eidlib.BEID_Exit();

	        System.out.println("<closed> Reading finished");
	        
	}
	
	public static ImageIcon getPhoto(){
		return ic;
	}
    public static String getNom() {
		return nom;
	}

	public static String getPrenom() {
		return prenom;
	}

	public static String getLieu_naiss() {
		return lieu_naiss;
	}

	public static String getDate_naiss() {
		return date_naiss;
	}

	public static String getSexe() {
		return sexe;
	}

	public static String getNationalite() {
		return nationalite;
	}

	public static String getReg_nat() {
		return reg_nat;
	}

	public static String getRue() {
		return rue;
	}

	public static String getCode_postal() {
		return code_postal;
	}

	public static String getPays() {
		return pays;
	}
	
    public static String FormatDate(String szDate){
        String szDateFormated = new String();
        
        szDateFormated = szDate.substring(6,8) + "/" + szDate.substring(4,6) + "/" + szDate.substring(0,4);
        
        return szDateFormated;
    }
    
    public static void closeReader(){
    	eidlib.BEID_Exit();
    }
}
